package org.fing.edu.uy.esb.adapative.ws.testing;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import org.fing.edu.uy.esb.adaptative.ws.utils.MBeanServicesConstants;
import org.fing.edu.uy.esb.adaptative.ws.utils.MgrConfig;


public class PrintName {

	public static void printMethodName(){
		String className = new Exception().getStackTrace()[1].getClassName();
		System.out.println("Service name -> " + className);	
		
		if(className.contains("BPS")){
			callAdminConsole(1l);
		}else if (className.contains("DGI")){
			callAdminConsole(2l);
		}else{
			callAdminConsole(3l);
		}		
		
	}
	
	public static void callAdminConsole(long serviceId){
		//String jmxServiceURL = MgrConfig.getInstance().getProperty(ConfigConstants.JMXEsbAdpURL);
		///String mBeanName = JMXServiceConstants.JMXAdmAdaptationMBean;
		
		String jmxServiceURL = MgrConfig.getInstance().getProperty("jmx.service.url.esbadp");
		String mBeanName = MBeanServicesConstants.serviceAgentName + MBeanServicesConstants.adminMBeanService;
		
		Object[] arguments = {serviceId};
		String[] signature = {Long.class.getName()};
		
		//invocar al servicio
		try {
			callOperation(jmxServiceURL, mBeanName, "reportInvoke", arguments, signature);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}				
	}
	
	public static Object callOperation(String jmxServiceURL, String mBeanName, String OperationName, Object[] arguments, String[] signature) throws Exception {
        JMXServiceURL service = new JMXServiceURL(jmxServiceURL);
        JMXConnector connector = JMXConnectorFactory.connect(service);
        MBeanServerConnection mbeanServer = connector.getMBeanServerConnection();
        ObjectName objectName = new ObjectName(mBeanName);
        Object object = mbeanServer.invoke(objectName, OperationName, arguments, signature);
        connector.close();
        return object;
	}
	
}
